package com.atguigu.tingshu.user.executor;



import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class UserJobExecutor {
    @Autowired
    UserInfoService userInfoService;
    private static Logger logger = LoggerFactory.getLogger(UserJobExecutor.class);

    //一天更新一次
    @XxlJob("updateVipStatus") //更新vip到期状态
    @Transactional(rollbackFor = Exception.class)
    public void updateVipStatus(){
        logger.info("更新vip到期状态：{}" , new Date());
        XxlJobHelper.log("更新vip到期状态：{}" , new Date());
        userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class)
                .eq(UserInfo::getIsVip, 1)
                .lt(UserInfo::getVipExpireTime, new DateTime().withTimeAtStartOfDay())
                .set(UserInfo::getIsVip , 0));
        logger.info("更新vip到期状态成功：{}" , new Date());
        XxlJobHelper.log("更新vip到期状态成功：{}", new Date());

    }
}
